#include "interrupt.h"
#include "display.h"
#include "button.h"

unsigned int count_pressing;


// Timer_A3 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A0_A1(void)
{
 switch( TA0IV )
 {
   case  2:
	   	   	   if ((isButtonSpeedUpPressing())||(isButtonSpeedDownPressing())||(isButtonTimerUpPressing())||(isButtonTimerDownPressing()))// overflow
	   			{
	   	   		   if (flag_ButtonPressing==0) flag_ButtonPressing = 1;
	   	   		   if (flag_ButtonPressing)
	   	   			{
	   	   				if (count_pressing<=1000)
	   	   				{
	   	   					count_pressing++;
	   	   				}
	   	   				else
	   	   				{
	   	   					count_pressing = 0;
	   	   					flag_ButtonPressing = 0;
	   	   					flag_ButtonHolded = 1;
	   	   				}
	   	   			}

	   				if (isButtonSpeedUpPressing())	ButtonPressed = BUTTON_SPEED_UP_PRESSED;
	   				if (isButtonSpeedDownPressing())	ButtonPressed = BUTTON_SPEED_DOWN_PRESSED;
	   				if (isButtonTimerUpPressing())	ButtonPressed = BUTTON_TIMER_UP_PRESSED;
	   				if (isButtonTimerDownPressing())	ButtonPressed = BUTTON_TIMER_DOWN_PRESSED;
	   			}
	   			else
	   			{
	   				if (flag_ButtonPressing)
	   				{
	   					count_pressing = 0;
	   					flag_ButtonPressing = 0;
	   					flag_ButtonPressed = 1;
	   				}
	   			}
	   	   	   	ADC10CTL0 &= ~ENC;                      //
	   	   	    while (ADC10CTL1 & BUSY);               // Wait if ADC10 core is active
	   	   	    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start

			break;                          // CCR1
   case  4:
	   	   	if (CCTL2 & CCI)
	   	   	{
	   	   		CCR2 += speed;
	   	   	}
	   	   	else
	   	   	{
	   	   		CCR2 += 1000-speed;
	   	   	}
	   	   	if (CCR2>10000)	CCR2 -= 10000;
	   	    break;                          // CCR2 not used
   case 10: 
            break;
 }
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0_A0 (void)
{
	if (pos<=3)
	{
		if (disp==0)  	DispOneCharOnePos(n+pos,pos);
		else DispOneCharOnePos(&disp,pos);
	   	pos++;
	}
	else
	{
		pos = 0;
	}
}

// Timer A1 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer_A1_A0 (void)
{
  flag_1s = 1;
  TA1CCR0 += 32767;                            // Add Offset to CCR0
}
